package com.sgcc.pda.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;


import com.sgcc.pda.hzaidl.impl.ScannerManager;
import com.sgcc.pda.tools.LogPrintUtils;
import com.sgcc.platforminterface.R;

import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import cn.bingoogolapple.qrcode.core.QRCodeView;
import cn.bingoogolapple.qrcode.zbar.ZBarView;

/**
 * 用ZBar扫码界面
 *
 * @Author jianiuqi
 * @Date 2020-05-19
 */
public class ZBarActivity extends Activity implements QRCodeView.Delegate {

    private ZBarView mZBarView;
    private ScannerManager.CameraConfig config;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        config = ScannerManager.getInstance().getConfig();
        setContentView(R.layout.activity_zbar);
        initView();
    }

    /**
     * 初始化View控件
     */
    private void initView() {
        mZBarView = findViewById(R.id.zbarview);
        mZBarView.setDelegate(this);
        findViewById(R.id.ll_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sendOrderedBroadcast("");
                finish();
            }
        });
        //闪光灯控制
        CheckBox cbFlashLamp = findViewById(R.id.cb_flash_lamp);
        cbFlashLamp.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    mZBarView.openFlashlight();
                } else {
                    mZBarView.closeFlashlight();
                }
            }
        });
        if (null != config) {
            config.onCreate(mZBarView.getScanBoxView(), mZBarView.getCameraPreview(), cbFlashLamp);
        }
    }

    /**
     * 发送识别结果
     *
     * @param data
     */
    private void sendOrderedBroadcast(String data) {
        Intent intent = new Intent();
        //设置广播的名字（设置Action）
        intent.setAction(ScannerManager.BROADCAST_PERMISSION_ZXING);
        intent.putExtra(ScannerManager.EXTRA_DATA, data);
        LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
    }

    @Override
    public void onScanQRCodeSuccess(String result) {
        sendOrderedBroadcast(result);
        LogPrintUtils.e("扫描到结果：" + result);
        ZBarActivity.this.finish();
    }

    @Override
    public void onCameraAmbientBrightnessChanged(boolean isDark) {
        String tipText = mZBarView.getScanBoxView().getTipText();

        String tip = "\n环境过暗，请打开闪光灯";
        String ambientBrightnessTip = "";

        if (null != config) {
            ambientBrightnessTip = config.provideOnCameraAmbientBrightnessChangedTips();
        }
        ambientBrightnessTip = TextUtils.isEmpty(ambientBrightnessTip) ? tip : ambientBrightnessTip;
        if (isDark) {
            if (!tipText.contains(ambientBrightnessTip)) {
                mZBarView.getScanBoxView().setTipText(tipText + ambientBrightnessTip);
            }
        } else {
            if (tipText.contains(ambientBrightnessTip)) {
                tipText = tipText.substring(0, tipText.indexOf(ambientBrightnessTip));
                mZBarView.getScanBoxView().setTipText(tipText);
            }
        }
    }

    @Override
    public void onScanQRCodeOpenCameraError() {
        LogPrintUtils.e("打开相机出错");
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        sendOrderedBroadcast("");
        finish();
    }

    @Override
    protected void onStart() {
        super.onStart();
        mZBarView.startCamera(); // 打开后置摄像头开始预览，但是并未开始识别
        mZBarView.startSpotAndShowRect(); // 显示扫描框，并开始识别
    }

    @Override
    protected void onStop() {
        mZBarView.stopCamera(); // 关闭摄像头预览，并且隐藏扫描框
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        mZBarView.onDestroy(); // 销毁二维码扫描控件
        super.onDestroy();
    }
}
